package coveranalyzer.handlers;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;

public class Tools {

	public static String IType2String(IType type) {
		StringBuffer rst = new StringBuffer(type.getJavaProject()
				.getElementName());
		rst.append("#");
		rst.append(type.getPackageFragment().getElementName());
		rst.append(".");
		StringBuffer typeName = new StringBuffer(type.getElementName());
		IJavaElement parent = type.getParent();
		while (!(parent instanceof ICompilationUnit)) {
			if (parent instanceof IType) {
				typeName.insert(0, '$');
				typeName.insert(0, parent.getElementName());
			}
			parent = parent.getParent();
		}
		rst.append(typeName);
		return rst.toString();
	}

	public static String IMethod2String(IMethod method) {
		IJavaElement parent = method.getParent();
		while (!(parent instanceof IType)) {
			parent = parent.getParent();
		}
		String signature = "";
		try {
			signature = method.getSignature();
		} catch (JavaModelException e) {
			e.printStackTrace();
		}
		return String.format("%s#%s%s", IType2String((IType) parent), method
				.getElementName(), signature);
	}

	public static String readFile(String path) {
		StringBuffer strb = new StringBuffer("");
		try {
			BufferedReader br = new BufferedReader(new FileReader(path));
			String line = br.readLine();
			while (line != null) {
				strb.append(line).append("\n");
				line = br.readLine();
			}
			br.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return strb.toString();
	}

	public static boolean isJavaProject(IProject project) {
		try {
			return project.isNatureEnabled("org.eclipse.jdt.core.javanature");
		} catch (CoreException e) {
			return false;
		}
	}

	public static IType resolveParentType(IJavaElement elem) {
		IJavaElement parent = elem;
		while (parent != null
				&& !((parent instanceof IType) && (parent.getParent() instanceof ICompilationUnit))) {
			parent = parent.getParent();
		}
		return parent == null ? null : (IType) parent;
	}

	public static IMethod resolveParentMethod(IJavaElement elem) {
		IJavaElement parent = elem;
		while (parent != null
				&& !((parent instanceof IMethod) && (parent.getParent() instanceof IType))) {
			parent = parent.getParent();
		}
		return parent == null ? null : (IMethod) parent;
	}

	public static IJavaElement resolveAnalysableUnit(IJavaElement elem) {
		IJavaElement parent = elem;
		List<IJavaElement> parents = new ArrayList<IJavaElement>();
		while (parent != null && !(parent instanceof ICompilationUnit)) {
			if (parent instanceof IMethod || parent instanceof IType) {
				parents.add(parent);
			}
			parent = parent.getParent();
		}
		int pos = 0;
		for (int i = 0; i < parents.size(); i++) {
			if ("".equals(parents.get(i).getElementName())) {
				pos = i + 1;
			}
		}
		return pos < parents.size() ? parents.get(pos) : null;
	}

}
